//************************************************************
//
// SUM_STATA.  REACTION FUNCTION WITH MEDIAN FIRM
//
// AUTHOR: LIONEL NESTA
//
// DATASOURCE: USING final_reaction_functions.dta FROM DATASTEP 5
//
//*************************************************************


// PREAMBLE
*set processors 24

clear
clear matrix 
clear mata
set more off
set mem 600m
set matsize 800


pwd


cd ..\DATA


// OPENING FILE
use sigma_2_digit_gr_corr.dta, clear
keep gvkey1
rename gvkey1 gvkey
duplicates drop

mmerge gvkey  using fin_var.dta, type(1:n) unmatched(master)
drop if _merge==1
mmerge gvkey year using firm_patprod.dta, type(n:1) unmatched(master)
drop if _merge == 1
drop _merge

drop if year < 1979
drop if year > 2005

gen isic = floor(dnum / 100)

table isic , c(count gvkey sum patent)

label define indus ///
	   25 "Furniture And Fixtures" ///
       26 "Paper And Allied Products" ///
       27 "Printing, Publishing, And Allied Industries" ///
       28 "Chemicals And Allied Products" ///
       29 "Petroleum Refining And Related Industries" ///
       30 "Rubber And Miscellaneous Plastics Products" ///
       32 "Stone, Clay, Glass, And Concrete Products" ///
       33 "Primary Metal Industries" ///
	   34 "Fabricated Metal Products, Except Machinery And Transportation Equipment" ///
	   35 "Industrial And Commercial Machinery And Computer Equipment" ///
	   36 "Electronic And Other Electrical Equipment And Components, Except Computer Equipment" ///
	   37 "Transportation Equipment" ///
	   38 "Measuring, Analyzing, And Controlling Instruments; Photographic, Medical And Optical Goods; Watches And Clocks" ///
	   39 "Miscellaneous Manufacturing Industries" ///
       42 "Motor Freight Transportation And Warehousing" ///
       48 "Communications" ///
       49 "Electric, Gas, And Sanitary Services" ///
       51 "Wholesale Trade-non-durable Goods" ///
       70 "Hotels, Rooming Houses, Camps, And Other Lodging Places" ///
       72 "Personal Services" ///
       73 "Business Services" ///
       87 "Engineering, Accounting, Research, Management, And Related Services" ///
	   99 "Others"
	
label values isic indus

gen isic2 = isic
replace isic2 = 26 if isic == 27
replace isic2 = 30 if isic == 32
replace isic2 = 30 if isic == 39
replace isic2 = 33 if isic == 34
replace isic2 = 55 if isic == 42
replace isic2 = 55 if isic == 49
replace isic2 = 55 if isic == 51
replace isic2 = 55 if isic == 70
replace isic2 = 55 if isic == 72
replace isic2 = 50 if isic == 48
replace isic2 = 50 if isic == 73
replace isic2 = 50 if isic == 87
replace isic2 = 99 if isic2 == 55

label define indus2 ///
	   25 "Furniture And Fixtures" ///
       26 "Paper Products, Printing & Publishing" ///
       28 "Chemicals And Allied Products" ///
       29 "Petroleum Refining" ///
       30 "Rubber, Concrete & Misc. Products" ///
       33 "Metal Industries" ///
	   35 "Industrial Machinery And Computer Equipment" ///
	   36 "Electronic Equipment" ///
	   37 "Transportation Equipment" ///
	   38 "Photographic, Medical And Optical Goods" ///
       50 "Business Services" ///
	   99 "Others"
   
	   
label values isic2 indus2

gen liq_ratio = liquid / current_liabilities
gen Herf = msh * msh
replace msh = msh * 100
gen rdpatsm = rd / patsmooth

preserve

	keep isic2 gvkey
	duplicates drop
	collapse (count) Nfirms = gvkey, by(isic2)
	save tmpfile.dta, replace

restore

mmerge isic2 using tmpfile.dta, type(n:1)
decode isic2, gen(Industry)


preserve
	collapse (mean) Nfirms (count) Nobs = gvkey (mean) RD = rd  Sales = y Asset = ppe_gross Liq_Ratio = liq_ratio RD_COST = rdpatsm Patent = patent, by(isic2 Industry)

	gen RD_INT = RD / Sales
	local listvar "RD Sales Asset Patent"
	foreach x of local listvar {
		 replace `x' = round(`x', .1)
		 format `x' %12.1fc  
	}
	
	local listvar "Nfirms Nobs"
	foreach x of local listvar {
		 format `x' %12.0fc 
	}

	local listvar "RD_INT  Liq_Ratio  RD_COST "
	foreach x of local listvar {
		 replace `x' = round(`x', .001)
		 format `x' %12.2fc 
	}

	save des_ind.dta, replace
restore

preserve

	keep gvkey
	duplicates drop
	collapse (count) Nfirms = gvkey
	sum Nfirms
	local nf = r(mean)

restore

	replace Nfirms = `nf'
	replace isic2 = 100
	replace Industry = "All Sectors"
	 
	collapse (mean) Nfirms (count) Nobs = gvkey (mean) RD = rd  Sales = y Asset = ppe_gross Liq_Ratio = liq_ratio RD_COST = rdpatsm  Patent = patent, by(isic2 Industry)

	gen RD_INT = RD / Sales
	local listvar "RD Sales Asset Patent"
	foreach x of local listvar {
		 replace `x' = round(`x', .1)
		 format `x' %12.1fc 
	}
	
	local listvar "Nfirms Nobs"
	foreach x of local listvar {
		 format `x' %12.0fc 
	}


	local listvar "RD_INT  Liq_Ratio  RD_COST "
	foreach x of local listvar {
		 replace `x' = round(`x', .001)
		 format `x' %12.3fc 
	}
	


append using des_ind.dta

sort isic2
gen code = _n
drop isic2
sort code
order code  Industry Nfirms Nobs RD Sales Asset Liq_Ratio RD_INT Patent RD_COST
drop code

cd ..\RESULTS

dataout, save(ind_table) word tex excel replace 


// TABLE 2
// OPENING FILE

use ..\DATA\final_reaction_functions.dta, clear
set more off

rename c_techno_c beta

rename c_market_c sigma


// DROPS ALL RF OUTSIDE CORNERS 1 TO 4 IN BETA-SIGMA SPACE 

gen case = case1

drop if case == .
collapse (median) lnrd1 lnrd2 beta  sigma  lnk1 lnliq_ratio1 msh1 msh12 lnrdpatsm1 lnk2 lnliq_ratio2 msh2 lnrdpatsm2 L1lnrd1 L1lnrd2 L1lnk1 L1lnliq_k1 L1msh1 L2lnrdpatsm1 L2lnrdpatsm2, by(gvkey1 year case)

tabulate year, gen(year_)


egen fcid  = group(gvkey1 case)
tsset fcid year

* SUMMARY STATISTICS

preserve

local listvar "lnrd1 lnrd2 beta  sigma  lnk1 lnliq_ratio1 msh1 lnrdpatsm1" 
*** set im the des tables**

gen Variable = ""
gen Observations = .
gen Mean = .
gen Median = .
gen Standard_deviation = .
gen Minimum = .
gen Maximum = .

local i = 1

foreach x of local listvar {
	sum `x', detail
	replace Variable = "`x'" in `i'
	replace Obs = r(N) in `i'
	replace Mean = r(mean) in `i'
	replace Median = r(p50) in `i'
	replace Standard_deviation = r(sd) in `i'
	replace Minimum = r(min) in `i'
	replace Maximum = r(max) in `i'
	
	local i = `i'  + 1
}

gen Case = 0 
keep Case Variable Obs Mean Median Standard_deviation Minimum Maximum
order Case Variable Obs Mean Median Standard_deviation Minimum Maximum
drop if _n > `i'

save tmpfile_0.dta, replace

restore


* SUMMARY STATISTICS

forvalues f = 1(1)4 {

	preserve
	keep if case == `f'

	local listvar "lnrd1 lnrd2 beta  sigma  lnk1 lnliq_ratio1 msh1 lnrdpatsm1" 
	*** set im the des tables**

	gen Variable = ""
	gen Observations = .
	gen Mean = .
	gen Median = .
	gen Standard_deviation = .
	gen Minimum = .
	gen Maximum = .

	local i = 1

	foreach x of local listvar {
		sum `x', detail
		replace Variable = "`x'" in `i'
		replace Obs = r(N) in `i'
		replace Mean = r(mean) in `i'
		replace Median = r(p50) in `i'
		replace Standard_deviation = r(sd) in `i'
		replace Minimum = r(min) in `i'
		replace Maximum = r(max) in `i'
		
		local i = `i'  + 1
	}

	gen Case = `f' 
	keep Case Variable Obs Mean Median Standard_deviation Minimum Maximum
	order Case Variable Obs Mean Median Standard_deviation Minimum Maximum
	drop if _n > `i'

	save tmpfile_`f'.dta, replace

	restore
}



use tmpfile_0.dta, clear

forvalues i = 1(1)4  {
	append using tmpfile_`i'.dta
}

local listvar "Mean Median Standard_deviation Minimum Maximum"
foreach x of local listvar {
	 replace `x' = round(`x', .001)
}
	
dataout, save(desc_table) word tex excel replace

clear

forvalues i = 1(1)4  {
	erase tmpfile_`i'.dta
}

/******************************
PREPARING FILE FOR GRAPH
*******************************/
cd ..\DO_FILES
use ..\DATA\sigma_2_digit_gr_corr.dta, clear
set more off

local ndc = 20

mmerge gvkey1 gvkey2 year using ..\DATA\beta_nozeros.dta, type(1:1)
keep if _merge == 3
drop _merge
drop if gvkey1 == gvkey2

*rename  c_techno_c beta

drop if abs(beta) >= 1

drop if abs(sigma) >= 1

egen cid = group(gvkey1 gvkey2)

egen cos_tek  = cut(beta), at(-1,-.9,-.8,-.7,-.6,-.5,-.4,-.3,-.2,-.1,0,.1,.2,.3,.4,.5,.6,.7,.8,.9,1, 1.1)
egen cos_mak  = cut(sigma), at(-1,-.9,-.8,-.7,-.6,-.5,-.4,-.3,-.2,-.1,0,.1,.2,.3,.4,.5,.6,.7,.8,.9,1, 1.1)

collapse (count) nobs = cid, by(cos_tek cos_mak)
gen lnnobs = ln(nobs)

drop if cos_mak ==.

seq n

mmerge n using ..\DATA\dyads.dta, type(1:1)


cd ..\RESULTS

twoway (contour lnnobs cos_mak cos_tek, interp(shepard) clegend(off) xsize(5) ysize(5) level(10) crule(chue) ccolors(gs15 gs14 gs13 gs12 gs11 gs10 gs9 gs8 gs7 gs6) zlabels(0(1)10)) ///
	(scatter m_m m_t, mcolor(black) msize(large) msymbol(smx) mlabel(name) mlabcolor(black) mlabposition(6)), ///
	ytitle({it:{&sigma}}) ytitle(, size(medlarge)) ylabel(#5, labsize(medsmall) nogrid) ///
	xtitle({it:{&beta}})  xtitle(, size(medlarge)) ///
	xlabel(#5, labsize(small)) legend(off) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white) margin(0 7 0 7) )
graph export sigma_beta_plot.pdf, as(pdf) replace 

